package com.lanxin.jet.controllers;

import java.util.Map;

import javax.servlet.http.HttpSession;

import com.lanxin.jet.beans.CommentBean;
import com.lanxin.jet.beans.UserTokenBean;
import com.lanxin.jet.services.CommentService;
import com.lanxin.jet.services.UserService;
import com.lanxin.jet.tools.JetDate;
import com.lanxin.jet.tools.JetResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    /**
     * 提交评论
     * @param uid       用户id
     * @param token     令牌
     * @param bid       关联id
     * @param content   评论内容
     * @param response
     */
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public Map<String, Object> add(String uid, String token, String bid, String content) {
        JetResponse jr = new JetResponse();
        if (userService.checkToken(new UserTokenBean(uid, token))) {
            jr.success(commentService.insert(content, bid, uid));
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 统计评论数量
     * @param bid
     * @param status
     * @param keyword
     * @param response
     */
    @RequestMapping(value = "/getCount.do", method = RequestMethod.POST)
    public Map<String, Object> getCount(String bid, int status, String keyword) {
        JetResponse jr = new JetResponse();
        jr.success(commentService.count(bid, status, keyword));
        return jr.map();
    }

    /**
     * 获取评论列表
     * @param offset    分页位置
     * @param limit     分页偏移量
     * @param bid       关联id
     * @param status    状态
     * @param keyword   关键词
     * @param response
     */
    @RequestMapping(value = "/getList.do", method = RequestMethod.POST)
    public Map<String, Object> getList(int offset, int limit, String bid, int status, String keyword) {
        JetResponse jr = new JetResponse();
        jr.success(commentService.getList(offset, limit, bid, status, keyword));
        return jr.map();
    }

    /**
     * 回复评论
     * @param session
     * @param response
     * @param id
     * @param reply
     */
    @RequestMapping(value = "/replyComment.do", method = RequestMethod.POST)
    public Map<String, Object> replyComment(HttpSession session, String id, String reply) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            if (!id.isEmpty()) {
                CommentBean comment = commentService.get(id);
                if (comment != null) {
                    comment.setReply(reply);
                    comment.setReplyTime(JetDate.timeNow());
                    commentService.update(comment);
                    jr.success();
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 设置评论状态
     * @param session
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping(value = "/setStatus.do", method = RequestMethod.POST)
    public Map<String, Object> setStatus(HttpSession session, String id, int status) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            if (!id.isEmpty()) {
                CommentBean comment = commentService.get(id);
                if (comment != null) {
                    comment.setStatus(status);
                    commentService.update(comment);
                    jr.success();
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 删除评论
     * @param id
     * @param response
     */
    @RequestMapping(value = "/delete.do")
    public Map<String, Object> delete(String id, HttpSession session) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            commentService.delete(id);
            jr.success();
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }
}